package com.allways.dubbo.mdm.service.dealer.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.dealer.Dealer;
import com.allways.dubbo.mdm.service.dealer.IDealerService;

/**
 * <p>ClassName: IDealerService</p>
 * <p>Description: 经销商Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-09-29</p>
 */
@Service("mdmDealerService")
public class DealerServiceImpl extends MdmBaseDaoImpl implements IDealerService {
	Log log = LogFactory.getLog(DealerServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addDealer(Dealer dealer) {
   		log.debug("For the new dealer parameters:"+ dealer.toJson());
   		validColumnLen(dealer);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(dealer.getDealerCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getDealerByCode(dealer.getDealerCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("dealer.insertDealer",dealer);
    }

    @Override
    public int updateDealer(Dealer dealer) {
    	log.debug("Update for the dealer parameters:"+ dealer.toJson());
    	validColumnLen(dealer);
     	//无code的不能更新
        if(StringUtils.isEmpty(dealer.getDealerCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Dealer oldDealer = getDealerByCode(dealer.getDealerCode());
        if(null == oldDealer){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldDealer.setDealerId(dealer.getDealerId());
		oldDealer.setCompanyCode(dealer.getCompanyCode());
		oldDealer.setDealerCode(dealer.getDealerCode());
		oldDealer.setDealerName(dealer.getDealerName());
		oldDealer.setNationCode(dealer.getNationCode());
		oldDealer.setProvinceCode(dealer.getProvinceCode());
		oldDealer.setCityCode(dealer.getCityCode());
		oldDealer.setDistrictCode(dealer.getDistrictCode());
		oldDealer.setStreet(dealer.getStreet());
		oldDealer.setAddress(dealer.getAddress());
		oldDealer.setPostcode(dealer.getPostcode());
		oldDealer.setContact(dealer.getContact());
		oldDealer.setMobiPhone(dealer.getMobiPhone());
		oldDealer.setTel(dealer.getTel());
		oldDealer.setFax(dealer.getFax());
		oldDealer.setLatitude(dealer.getLatitude());
		oldDealer.setLongitude(dealer.getLongitude());
		oldDealer.setDataSource(dealer.getDataSource());
		oldDealer.setDisplay(dealer.getDisplay());
		oldDealer.setDeleteMark(dealer.getDeleteMark());
		oldDealer.setCreateUser(dealer.getCreateUser());
		oldDealer.setCreateTime(dealer.getCreateTime());
		oldDealer.setUpdateUser(dealer.getUpdateUser());
		oldDealer.setUpdateTime(dealer.getUpdateTime());
        
        return update("dealer.updateDealer",oldDealer);
    }

    @Override
    public int deleteDealer(Dealer dealer) {
    	log.debug("Delete for the dealer parameters:"+ dealer.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(dealer.getDealerCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("dealer.deleteDealer",dealer);
    }

    @Override
    public Dealer getDealerByCode(String code) {
    	log.debug("Query for dealer code:" + code);
        return queryForObject("dealer.getDealerByCode",code);
    }
    @Override
    public long getDealerCount(Dealer dealer) {
    	log.debug("Query for dealer count parameters:" + dealer.toJson());
        return queryForObject("dealer.getDealerCount",dealer);
    }
   @Override
    public List<Dealer> queryDealers(Dealer dealer, int firstRow, int lastRow) {
      	log.debug("Query for dealer parameters:" + dealer.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("dealer",dealer);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("dealer.queryDealers", param);
    }
    
    /**
     * 验证字段长度
     * @param dealer 需要验证的对象
     */
    private void validColumnLen(Dealer dealer){
		if(!StringUtils.isEmpty(dealer.getDealerId()) && dealer.getDealerId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(dealer.getCompanyCode()) && dealer.getCompanyCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(dealer.getDealerCode()) && dealer.getDealerCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(dealer.getDealerName()) && dealer.getDealerName().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(dealer.getNationCode()) && dealer.getNationCode().length()>2){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{2},null);
        }
		if(!StringUtils.isEmpty(dealer.getProvinceCode()) && dealer.getProvinceCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(dealer.getCityCode()) && dealer.getCityCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(dealer.getDistrictCode()) && dealer.getDistrictCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(dealer.getStreet()) && dealer.getStreet().length()>30){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{30},null);
        }
		if(!StringUtils.isEmpty(dealer.getAddress()) && dealer.getAddress().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(dealer.getPostcode()) && dealer.getPostcode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(dealer.getContact()) && dealer.getContact().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(dealer.getMobiPhone()) && dealer.getMobiPhone().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(dealer.getTel()) && dealer.getTel().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(dealer.getFax()) && dealer.getFax().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(dealer.getLatitude()) && dealer.getLatitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(dealer.getLongitude()) && dealer.getLongitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(dealer.getDisplay()) && dealer.getDisplay().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(dealer.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
